package com.pan.kang.controller;

import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.MapSearcher;
import com.ejlchina.searcher.SearchResult;
import com.ejlchina.searcher.util.MapUtils;
import com.pan.kang.bean.EmployeeSearcherBean;
import com.pan.kang.common.Request;
import com.pan.kang.common.Response;
import com.pan.kang.common.Transfer;
import com.pan.kang.dto.EmployeeDto;
import com.pan.kang.vo.EmployeeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/employee")
public class EmployeeController {


    @Autowired
    private BeanSearcher beanSearcher;

    @Autowired
    private MapSearcher mapSearcher;

    @Autowired
    private Transfer transfer;

    /**
     * Tang Li 员工名称查询
     */
    @PostMapping("/getEmployeeByName")
    public Response<List<EmployeeVo>> create(@RequestBody Request<EmployeeDto> requestBean) {
        EmployeeDto employeeDto = requestBean.getData();
        String name = employeeDto.getName();
        Map<String, Object> params = MapUtils.builder().field(EmployeeSearcherBean::getName, name).build();
        SearchResult<EmployeeSearcherBean> searchResult = beanSearcher.search(EmployeeSearcherBean.class, params);
        List<EmployeeSearcherBean> searcherBeanList = searchResult.getDataList();
        List<EmployeeVo> employeeVoList = transfer.searcherBeanListToVoList(searcherBeanList);
        return new Response<>(employeeVoList);
    }


    /*

    请求入参

    {
    "data": {
        "name": "Tang Li"
        }
    }

    请求响应

    {
        "data": [
            {
                "id": 12,
                "name": "Tang Li",
                "gender": "Male",
                "age": 30,
                "entryDate": "2018-08-05T04:01:01.000+00:00",
                "departmentId": 6,
                "departmentName": "Maintenance"
            },
            {
            "id": 13,
            "name": "Tang Li",
            "gender": "Female",
            "age": 29,
            "entryDate": "2018-06-05T04:01:01.000+00:00",
            "departmentId": 4,
            "departmentName": "Manager"
            }
        ],
        "message": "请求成功",
        "code": 200
        }
    */

}
